<?php
/**
 * Created by PhpStorm.
 * User: godjarvis
 * Date: 2020/08/06
 * Time: 10:31
 */

namespace App\Model\Dao;

use App\Model\Entity\SearchHistory;
use Fresh\Facade\Db;
use Fresh\Mvc\Model\Dao;

class SearchHistoryDao extends Dao
{
    public function insert($row)
    {
        return Db::insert(SearchHistory::class, $row);
    }

    public function gets($dev_id)
    {
        return $this->expr()
            ->field('id, search_text text')
            ->from(SearchHistory::class)
            ->where('dev_id=?', $dev_id)
            ->where('deleted_at=0')
            ->orderBy('updated_at desc')
            ->query()
            ->find();
    }

    public function searchTextExistsId($text)
    {
        return $this->expr()
            ->from(SearchHistory::class)
            ->where('search_text=?', $text)
            ->where('deleted_at=0')
            ->query()
            ->pluck('id');
    }

    public function delete($dev_id, $ids)
    {
        $qb = $this->expr()->where('dev_id=?', $dev_id)->inWhere('id', $ids);
        return Db::update(SearchHistory::class, ['deleted_at' => time()], $qb);
    }

    public function update($row, $id)
    {
        $qb = $this->expr()->where('id=?', $id);
        return Db::update(SearchHistory::class, $row, $qb);
    }
}
